﻿@************************************************************************************//
// Autor:  		                								     				*//
//***********************************************************************************//
// Edit Masivo()		Acceso ADMINISTRADOR			        					*//
// Vista que devuelve todos los ítems de BI listados, paginados y con opción		*//
//  de búsqueda y edición 															*//
// _________________________________________________________________________________*@

<table id="jqgBIs" cellpadding="0" cellspacing="0"></table>
<div id="jqgpBIs" style="text-align:center;"></div>
<div id="dlgMarcaModelo"></div>
<div id="dlgncar"></div>

@section JavaScript {
    <script id="tmplMarcaModelo" type="text/x-jquery-tmpl">
        ${Marca}<br /><br />
        ${ClaseObjeto}<br />
        ${Modelo}
    </script>
    <script id="tmplncar" type="text/x-jquery-tmpl">
        ${Nombre}<br /><br />
        ${Cargo}<br />
        ${Ceco}<br />
        ${Division}
    </script>
    <script type="text/javascript">
        $.lastSelectedRow = null;

        $(document).ready(function () {
            $('#jqgBIs').jqGrid({
                //url from wich data should be requested
                url: '@Url.Action("BI")',
                //url for inline edit
                editurl: '@Url.Action("UpdateBI")',
                //event for inline edit
                onSelectRow: function (currentSelectedRow) {
                    if (currentSelectedRow && currentSelectedRow != $.lastSelectedRow) {
                        //save changes in row 
                        $('#jqgBIs').jqGrid('saveRow', $.lastSelectedRow, false);
                        $.lastSelectedRow = currentSelectedRow;
                    }
                    //trigger inline edit for row
                    $('#jqgBIs').jqGrid('editRow', $.lastSelectedRow, true);
                },
                //type of data
                datatype: 'json',
                //url access method type
                mtype: 'POST',
                //columns names
                colNames: ['Usuario', 'CargoId', 'Serial', 'Estado', 'Tipo Bien', 'Anexo/Factura', 'Fecha Adquisición', 'Marca/Modelo', 'División', 'Centro Costo'],
                //columns model
                colModel: [
                            { name: 'ncar', index: 'ncar', align: 'left', editable: false, edittype: 'text', formatter: ncarFormatter, width: 100 },
                            { name: 'CargoId', index: 'CargoId', align: 'left', editable: false, edittype: 'text', hidden: true },
                            { name: 'BienInformaticoId', index: 'BienInformaticoId', align: 'left', editable: true, edittype: 'text', editrules: { required: true }, editoptions: { readonly: 'readonly' }, width: 100 },
                            { name: 'EstadoId', index: 'EstadoId', align: 'left', editable: true, search: true, stype: 'select', searchoptions: { dataUrl: '@Url.Action("Estados")' }, edittype: 'select', editoptions: { dataUrl: '@Url.Action("Estados")' }, editrules: { required: true}, width: 145 },
                            { name: 'TipoBienId', index: 'TipoBienId', align: 'left', editable: true, search: true, stype: 'select', searchoptions: { dataUrl: '@Url.Action("TipodeBienes")' }, edittype: 'select', editoptions: { dataUrl: '@Url.Action("TipodeBienes")' }, editrules: { required: true}, width: 90 },
                            { name: 'nanf', index: 'nanf', align: 'left', editable: true, edittype: 'text', width: 40 },
                            { name: 'dfva', index: 'dfva', align: 'left', editable: true, formatter: 'date', edittype: 'text', width: 105 },
                            { name: 'MarcaModeloId', index: 'MarcaModeloId', align: 'left', editable: true, search: true, stype: 'select', searchoptions: { dataUrl: '@Url.Action("MarcaModelos")' }, edittype: 'select', editoptions: { dataUrl: '@Url.Action("MarcaModelos")' }, editrules: { required: true}, width: 115 },
                            { name: 'DivisionId', index: 'DivisionId', align: 'left', editable: true, search: true, stype: 'select', searchoptions: { dataUrl: '@Url.Action("Divisiones")' }, edittype: 'select', editoptions: { dataUrl: '@Url.Action("Divisiones")' }, editrules: { required: true }, width: 60 },
                            { name: 'ceco_id', index: 'ceco_id', align: 'left', editable: true, edittype: 'text', width: 90 }
                          ],
                //pager for grid
                pager: $('#jqgpBIs'),
                //number of rows per page
                rowNum: 20,
                //initial sorting column
                //No se toma en cuenta. Esta cableado
                //que se agrupe por BienInformaticoId
                sortname: 'BienInformaticoId',
                //initial sorting direction
                //Posibles valores: asc o desc
                sortorder: 'asc',
                //we want to display total records count
                viewrecords: true,
                //grid height
                height: '100%'
                // multiple selects 
                // multiselect: true
            });
            $('#jqgBIs').jqGrid('filterToolbar', {
                //we want filters to be returned the same way as with advanced searching
                stringResult: true
            });
            $('#dlgncar').dialog({ autoOpen: false, bgiframe: true, resizable: false, title: 'Usuario' });
            $('a[data-ncar-id]').live('click', function (e) {
                if (e.preventDefault)
                    e.preventDefault();
                else
                    e.returnValue = false;

                var dialogPosition = $(this).offset();
                $.post('@Url.Action("CargoId")', { cargoId: $(this).parent().next("td").attr('title') }, function (data) {
                    $('#dlgncar').empty();
                    $('#tmplncar').tmpl(data).appendTo('#dlgncar');
                    $('#dlgncar').dialog('option', 'position', [dialogPosition.left, dialogPosition.top]);
                    $('#dlgncar').dialog('open');
                });
            });
            function ncarFormatter(cellvalue, options, rowObject) {
                return "<a href='#' data-ncar-id='" + cellvalue + "'>" + cellvalue + "</a>";
            };
        });
    </script>
}